package com.lishe.lishe.lishe.module.myorder.presenter;

import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.myorder.bean.LogisticsBean;
import com.lishe.lishe.lishe.module.myorder.bean.OrderDetatilBean;
import com.lishe.lishe.lishe.module.myorder.bean.OrderListBean;
import com.lishe.lishe.lishe.module.myorder.interfase.OrederInterface;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shino on 2017/12/26.
 */

public class OrderPresenter {

    private static OrderPresenter orderPresenter;

    public static OrderPresenter getInstance() {
        if (orderPresenter == null) {
            orderPresenter = new OrderPresenter();
        }

        return orderPresenter;
    }

    /**
     * 获取购物车列表
     *
     * @param token
     * @param status
     * @param page
     * @param pageSize
     */
    public void getOrderList(String token, String status, String page, String pageSize, final DataResponseListener<OrderListBean> listener) {
        OrederInterface orederInterface = RetrofitFactory.create().create(OrederInterface.class);
        orederInterface.getOrderList(token, status, page, pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(OrderListBean bean) {
                        if (bean.getResult() == 100 && bean.getErrcode() == 0) {
                            listener.onSuccess(bean);
                        } else {
                            listener.onFail(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 取消订单
     * @param token
     * @param payment_id
     * @param listener
     */
    public void cancelOrder(String token, String payment_id,final DataResponseListener<BaseRequestBody> listener) {
        OrederInterface orederInterface = RetrofitFactory.create().create(OrederInterface.class);
        orederInterface.cancleOrder(token, payment_id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<Object> baseDataPageBodyBaseRequestBody) {
                        if (baseDataPageBodyBaseRequestBody.getResult() == 100 && baseDataPageBodyBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseDataPageBodyBaseRequestBody);
                        } else {
                            listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 确认收货
     * @param token
     * @param payment_id
     * @param listener
     */
    public void confirmOrder(String token, String payment_id,final DataResponseListener<BaseRequestBody> listener) {
        OrederInterface orederInterface = RetrofitFactory.create().create(OrederInterface.class);
        orederInterface.confirmOrder(token, payment_id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<Object> baseDataPageBodyBaseRequestBody) {
                        if (baseDataPageBodyBaseRequestBody.getResult() == 100 && baseDataPageBodyBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseDataPageBodyBaseRequestBody);
                        } else {
                            listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取物流信息
     */
    public void getExpressList(String tid, final DataResponseListener<LogisticsBean> listener) {
        OrederInterface orederInterface = RetrofitFactory.create().create(OrederInterface.class);
        orederInterface.getExpressList(tid, Globals.getToken()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LogisticsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LogisticsBean bean) {
                        if (bean.getResult() == 100 && bean.getErrcode() == 0) {
                            listener.onSuccess(bean);
                        } else {
                            listener.onFail(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 订单详情
     */
    public void getOrderDetail(String tid, String paymentId, final DataResponseListener<OrderDetatilBean> listener) {
        OrederInterface orederInterface = RetrofitFactory.create().create(OrederInterface.class);
        orederInterface.getOrderDetail(Globals.getToken(), tid, paymentId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderDetatilBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(OrderDetatilBean bean) {
                        if (bean.getResult() == 100 && bean.getErrcode() == 0) {
                            listener.onSuccess(bean);
                        } else {
                            listener.onFail(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
